
//
//  LoginMainController.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//
#import "LoginMainController.h"
#import "LoginQuicklyController.h"
#import "LoginForgetPwdController.h"
#import "LoginUserRegistController.h"
#import "BaseCommondNaviController.h"

@interface LoginMainController ()<LoginNormalCellDelegate>

@end

@implementation LoginMainController 
@synthesize goHome;



#pragma  mark - init
- (instancetype)init{
    if (self = [super init]) {
        self.configureStyle = kLocalConfigLogin;
        goHome = 0;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"";

}

- (void)backBtnPressed:(id) sender{
    
    if(goHome == 1){
        
        AppDelegate *applicatioin = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        UITabBarController *tab = (UITabBarController *)applicatioin.window.rootViewController;
        
        if(tab.selectedIndex != 0){
            
            [tab.selectedViewController popToRootViewControllerAnimated:YES];
            tab.selectedIndex = 0;
            
        }else{
            
             [tab.selectedViewController popToRootViewControllerAnimated:YES];
            
        }
       
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        
      [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    
}


+(id)createNav{
    
    LoginMainController * loginController = [[LoginMainController alloc] init];
    id navController = [[BaseCommondNavTool getInstance] createNav:NAV2 rootController:loginController];
    return navController;
    
}
+(id)createNavWithHome{
   
    LoginMainController * loginController = [[LoginMainController alloc] init];
    loginController.goHome = 1;
    id navController = [[BaseCommondNavTool getInstance] createNav:NAV2 rootController:loginController];
    return navController;

    
}
+(id)createNavWithTab{
    
    LoginMainController * loginController = [[LoginMainController alloc] init];
    loginController.goBack = 1;
    id navController = [[BaseCommondNavTool getInstance] createNav:NAV2 rootController:loginController];
    return navController;
    
}

-(id)createBackNoNav{
  UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [self.view addSubview:leftButton];
  

  leftButton.backgroundColor = [UIColor clearColor];
  
  [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.mas_equalTo(15);
    make.width.height.mas_equalTo(44);
  }];
//  [leftButton setImage:[UIImage imageNamed:@"back_icon" ] forState:UIControlStateNormal];
  [leftButton addTarget:self action:@selector(backPressed:)forControlEvents:UIControlEventTouchUpInside];
  
  [self.view layoutIfNeeded];
  
  
  LoginMainController * loginController = [[LoginMainController alloc] init];
  return loginController;
}



#pragma mark -
#pragma mark tableview datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSArray *array = [LoginMainAction getLocalConfig:self.configureStyle];
    return [array count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LoginNormalCell *cell = [LoginNormalCell loginNormalCell:tableView];
    cell.configDic = [[LoginMainAction getLocalConfig:self.configureStyle] objectAtIndex:indexPath.section];
    cell.userInput.delegate = self;
    cell.loginDelegate = self;
    cell.layoutTouserInput = self.configureStyle;
    if (indexPath.section == 1) {
        cell.userInput.secureTextEntry = YES;
    }
    
    [cell.getVerify addTarget:self action:@selector(verifyBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;

}


#pragma mark -
#pragma mark resign responder 

-(void)loginBtnPressed:(id)sender{
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * userName = cell.userInput.text;
    
    indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * passWord = cell.userInput.text;
    if (userName.length <= 0) {
        [self showToastAddWithTitle:@"手机号不能为空"];
        return;
    }else if (passWord.length <= 0) {
        [self showToastAddWithTitle:@"密码不能为空"];
        return;
    }
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction loginWithUserName:userName withPassWord:passWord withCallBlock:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
            [self showToastAddWithTitle:error_msg];
        }else {
            if (goBack) {
                [loginMainAction gorGoBackToMeViewController];
            }
          if (self.isRn) {
              UIApplication *application = [UIApplication sharedApplication];
              UIWindow *window = application.keyWindow;
             
              
              
              UIViewController * rootViewC= window.rootViewController;
            [rootViewC.navigationController popViewControllerAnimated:YES];
          }else{
            [self dismissViewControllerAnimated:YES completion:nil];
          }
        }
    }];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
        UITextField * userName = cell.userInput;
        indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
        UITextField * passWord = cell.userInput;
    
        if (textField == userName) {
            if ([passWord.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
                
                [loginMainView initLoginBtn];
                
            }else {
                [loginMainView activeLoginBtn];
            }
        }else {
            if ([userName.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
               
                [loginMainView initLoginBtn];
                
            }else {
                
                [loginMainView activeLoginBtn];
            }
     
        }
    return  YES;
}

- (void)clearContent:(LoginNormalCell *)cell{
    [loginMainView initLoginBtn];
}



@end
